import { base_url } from './config.js';
import { getToken } from '../util/tool.js';

export const user = {
	// 微信登录接口
	async wxLoginBycode() {
		let code = await new Promise((resolve, reject) => {
			uni.login({
				provider: 'weixin',
				success(loginRes) {
					console.log('【 Response in request wx.login 】', loginRes);
					resolve(loginRes.code);
				},
				fail(err) {
					uni.showToast({
						title: '网络错误',
						icon: 'error'
					});
					console.log('【 Error in request wx.login 】', err);
					reject('');
				}
			});
		});
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/users/wxLoginBycode',
				method: 'POST',
				data: {
					code,
				},
				header: {
					'content-type': 'application/json'
				},
				success(res) {
					if (res.data.success) {
						console.log('【 Response in request wxLoginBycode 】', res);
						uni.setStorageSync('jwt', res.data.jwt);
						uni.setStorageSync('uid', res.data.uid);
						uni.setStorageSync('userType', res.data.userType);
						resolve({ success: true });
					} else {
						uni.showToast({
							title: '网络错误',
							icon: 'error'
						});
						reject({ success: false });
					}
				},
				fail(err) {
					uni.showToast({
						title: '网络错误',
						icon: 'error'
					});
					console.log('【 Error in request wxLoginBycode 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 检查用户是否为登录态
	async checkLoginByJWT(jwt) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/users/checkLoginByJWT',
				method: 'POST',
				data: {
					jwt
				},
				header: {
					'content-type': 'application/json'
				},
				success(res) {
					console.log('【 Response in request checkLoginByJWT 】', res);
					resolve({ success: res.data.login });
				},
				fail(err) {
					console.log('【 Error in request checkLoginByJWT 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 查询用户是否同意了用户协议
	async checkUserAgreement() {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/users/checkUserAgreement',
				method: 'POST',
				data: {
					jwt
				},
				header: {
					'content-type': 'application/json'
				},
				success(res) {
					console.log('【 Response in request checkUserAgreement 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request checkUserAgreement 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 用户同意用户协议和隐私政策
	async agreeUserAgreement() {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/users/agreeUserAgreement',
				method: 'POST',
				data: {
					jwt
				},
				header: {
					'content-type': 'application/json'
				},
				success(res) {
					console.log('【 Response in request agreeUserAgreement 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request agreeUserAgreement 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 获取用户基本信息
	async getUserBaseInfo() {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/users/getUserBaseInfo',
				method: 'POST',
				data: {
					jwt
				},
				header: {
					'content-type': 'application/json'
				},
				success(res) {
					console.log('【 Response in request getUserBaseInfo 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request getUserBaseInfo 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 获取用户详细信息
	async getUserInfo() {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/userinfo/getUserInfo',
				method: 'GET',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request getUserInfo 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request getUserInfo 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 修改用户信息
	async updateUserInfo(username, college, major, level, bio) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/userinfo/updateUserInfo',
				method: 'POST',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				data: { username, college, major, level, bio },
				success(res) {
					console.log('【 Response in request updateUserInfo 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request updateUserInfo 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 获取 专注时长、投递数、收藏数 的接口
	async getHomePageNeedData() {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/userinfo/getHomePageNeedData',
				method: 'GET',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				success(res) {
					console.log('【 Response in request getHomePageNeedData 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request getHomePageNeedData 】', err);
					reject({ success: false });
				}
			});
		});
	},
	// 上传用户头像
	async updateUserIcon(img) {
		let jwt = getToken();
		return new Promise((resolve, reject) => {
			uni.request({
				url: base_url + '/userinfo/updateUserIcon',
				method: 'POST',
				header: {
					'content-type': 'application/json',
					'Authorization': jwt
				},
				data: { img },
				success(res) {
					console.log('【 Response in request updateUserIcon 】', res);
					resolve(res.data);
				},
				fail(err) {
					console.log('【 Error in request updateUserIcon 】', err);
					reject({ success: false });
				}
			});
		});
	},
};